/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpfActivity_Detail.java
 *
 * Created on Aug 13, 2010, 10:06:37 AM
 */

package GUI;

import config.Database_function;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author QUOCTHGC60050
 */
public class pnf_Manager_Activity extends javax.swing.JPanel {
Connection con= Database_function.setConnection();
        Statement lo;
    ResultSet re;
    Vector table = new Vector();
    Vector label= new Vector();

    public pnf_Manager_Activity() {

        initComponents();
        this.setSize(800, 400);
        initdetail_activiy();
        this.jTableChild.setVisible(false);
    }

    public void addTableN() // insert data into table
    {
        try
        {
            Statement st1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet ag =st1.executeQuery("select AgeGroup from tbl_AgeGroup where AgeGroupID like '"+re.getString("AgeGroupID")+"'");
            ag.first();

            Vector temp= new Vector();

            temp.add(re.getString("ActivityID"));
            temp.add(re.getString("ActivityName"));
            temp.add(re.getString("Fee"));
            temp.add(ag.getString("AgeGroup"));
            table.add(temp);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void getChildlist(String id) // get and insert Child List to table
    {
        try
        {
            this.jTableChild.setVisible(true);
            Statement lo1= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet re1=lo1.executeQuery("select * from tbl_Child where AgeGroupID like '"+id+"' order by 'ChildID' asc");
            Vector table1 = new Vector();
            Vector label1= new Vector();
            label1.add("ChildID");label1.add("FirstName");label1.add("LastName");label1.add("Gender");
            
            while(re1.next())
            {
                Vector temp= new Vector();
                temp.add(re1.getString("ChildID"));
                temp.add(re1.getString("FirstName"));
                temp.add(re1.getString("LastName"));
                temp.add(re1.getString("Gender"));
                table1.add(temp);
            }

            this.jTableChild.setModel(new DefaultTableModel (table1,label1));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

     public void initdetail_activiy() // initialize activity table
    {
        try
        {
            lo= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re=lo.executeQuery("select * from tbl_Activity order by 'ActivityID' asc");

            label.add("ActivityID");label.add("ActivityName");label.add("Fee");label.add("AgeGroup");

           while(re.next())
            {
                addTableN();
            }

            this.jTableActivity.setModel(new DefaultTableModel (table,label));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableActivity = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        }
        ;
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableChild = new javax.swing.JTable();

        setBackground(new java.awt.Color(227, 239, 255));

        jTableActivity.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "First Name", "Last Name", "AgeGroup", "Gender", "Address", "Status"
            }
        ));
        jTableActivity.setGridColor(new java.awt.Color(213, 213, 213));
        jTableActivity.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableActivityMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableActivity);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(213, 228, 242));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Child list:"));
        jPanel1.setOpaque(false);

        jTableChild.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ChildID", "FirstName", "LastName", "Gender"
            }
        ));
        jTableChild.setGridColor(new java.awt.Color(213, 213, 213));
        jScrollPane2.setViewportView(jTableChild);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 393, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 6, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 7, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 165, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 812, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(197, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAdd))
                        .addGap(139, 139, 139))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(213, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        // invoke add activity function and refresh table
        jdl_addActivity aa = new jdl_addActivity(null,true);
        aa.setVisible(true);
        table.removeAllElements();
            label.removeAllElements();
            initdetail_activiy();
}//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        // invoke edit activity function and refresh table
        int d=this.jTableActivity.getSelectedRow();// get selected row on table
        if(d>=0) {
            String idActivity=(String)this.jTableActivity.getValueAt(d, 0);
            jdl_Edit_Activity ea = new jdl_Edit_Activity(null,true,idActivity);
            ea.setVisible(true);
            table.removeAllElements();
            label.removeAllElements();
            initdetail_activiy();
        }else
            JOptionPane.showMessageDialog(this, "You must choose an activity to edit!","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/Exit.png")));
}//GEN-LAST:event_btnEditActionPerformed

    private void jTableActivityMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableActivityMouseClicked
        // TODO add your handling code here:
        // modify select method and invoke get child list function
        try{
        int selectedRow =  jTableActivity.getSelectedRow();
        if (evt.getClickCount()==2) {

            String agegroup =(String) jTableActivity.getValueAt(selectedRow, 3);
            Statement st2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ResultSet rs =st2.executeQuery("select AgeGroupID from tbl_AgeGroup where AgeGroup like '"+agegroup+"'");
            rs.first();
            
            getChildlist (rs.getString("AgeGroupID"));

        }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jTableActivityMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableActivity;
    private javax.swing.JTable jTableChild;
    // End of variables declaration//GEN-END:variables

}
